package cn.broadsense.common.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * Caffeine本地缓存
 *
 * @author: pengxin
 * @create: 2024-03-27 14:24
 * @Version 1.0
 **/
@Configuration
@EnableCaching
public class CaffeineCacheConfig {
    @Bean(value = "caffeineCache60")
    public CacheManager cacheManager() {
        CaffeineCacheManager caffeineCacheManager  =new CaffeineCacheManager();
        caffeineCacheManager.registerCustomCache("cf60",Caffeine.newBuilder()
                .maximumSize(100) // 设置缓存最大容量
                .expireAfterWrite(60, TimeUnit.MINUTES) // 设置缓存过期时间（写入后5分钟过期）
                .build());
        return caffeineCacheManager;
    }
}
